package com.samp.lucene.core;

import java.nio.file.Paths;
import java.util.Date;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.DateTools.Resolution;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

public class IndexSearcherDemo {
	
	public static void searchDoc(String indexDir, String field, String searchTerm, Integer rows){
		try {
			IndexReader ir = DirectoryReader.open(FSDirectory.open(Paths.get(indexDir)));
			IndexSearcher is = new IndexSearcher(ir);
			Query query = new TermQuery(new Term(field, searchTerm));
			Sort sort = getSortByType("field");
			TopDocs topDocs = is.search(query, rows, sort);
			ScoreDoc[] hits = topDocs.scoreDocs;
			String prifix = "    ";
			System.out.println("总共匹配记录总数："+topDocs.totalHits);
			System.out.println("当前返回记录总数："+hits.length);
			for(int i = 0 ; i < hits.length ; i++ ){
				System.out.println(prifix+"doc="+hits[i].doc+",score="+hits[i].score);
				Document doc = is.doc(hits[i].doc);
				printDoc(prifix, doc);
			}
			ir.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 指定排序规则。
	 * @param sortType
	 * @return
	 */
	private static Sort getSortByType(String sortType){
		Sort sort = null;
		if( "field".equals(sortType) ){ //指定字段排序
			SortField sortField = new SortField("id", SortField.Type.INT,false);
			sort = new Sort(sortField);
		}else if("index".equals(sortType) ){//根据索引排序
			sort = Sort.INDEXORDER;
		}else if("relevance".equals(sortType) ){ //根据相关性排序
			sort = Sort.RELEVANCE;
		}
		return sort;
	}
	
	private static void printDoc(String prifix, Document doc){
		String append="    ";
		System.out.println(prifix+append+"id="+doc.get("id"));
		System.out.println(prifix+append+"title="+doc.get("title"));
		System.out.println(prifix+append+"creator="+doc.get("creator"));
		Date date = new Date(Long.valueOf(doc.get("createTime")));
		System.out.println(prifix+append+"createTime="+doc.get("createTime"));
		System.out.println(prifix+append+"createTime="+DateTools.dateToString(date, Resolution.SECOND));
		System.out.println(prifix+append+"intNum="+doc.get("intNum"));
		System.out.println(prifix+append+"doubleNum="+doc.get("doubleNum"));
		Date lastModified =  new Date(Long.valueOf(doc.get("lastModified")));
		System.out.println(prifix+append+"lastModified="+DateTools.dateToString(lastModified, Resolution.SECOND));
		System.out.println(prifix+append+"path="+doc.get("path"));
	}
	
	
	public static void main(String[] args) {
		String indexDir = "D:\\nas\\samp\\lucene\\test01\\index";
		searchDoc(indexDir, "title","百度", 100);
	}

}
